<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统设置</el-breadcrumb-item>
      <el-breadcrumb-item>友链管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="30">
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加友链</el-button>
        </el-col>
      </el-row>
      <el-table :data="sites" stripe>
        <el-table-column label="序号" prop="id"></el-table-column>
        <el-table-column label="友链名称" prop="blogName"></el-table-column>
        <el-table-column label="头像" prop="avatar"></el-table-column>
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="网址" prop="url"></el-table-column>
        <el-table-column label="是否可用">
          <template v-slot:default="scope">
            <el-switch v-model="scope.row.added" @change="siteStateChange(scope.row.id)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="scope">
            <el-button type="primary" icon="el-icon-edit" @click="open_edit_site(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="deleteSite(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-size="size"
        layout="total, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <el-dialog
        title="添加友链"
        :visible.sync="addDialogVisible"
        @close="addDialogClosed"
        width="50%">
        <!--内容主体区域-->
        <el-form :model="SiteForm">
            <el-form-item label="友链名称">
              <el-input v-model="SiteForm.blogName"></el-input>
            </el-form-item>
            <el-form-item label="头像">
              <el-input v-model="SiteForm.avatar"></el-input>
            </el-form-item>
            <el-form-item label="友链描述">
              <el-input v-model="SiteForm.description"></el-input>
            </el-form-item>
            <el-form-item label="友链地址">
              <el-input v-model="SiteForm.url"></el-input>
            </el-form-item>
        </el-form>
        <!--内容底部区域-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addSite()">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog
        title="修改友链"
        :visible.sync="editDialogVisible"
        @close="editDialogClosed"
        width="50%">
        <!--内容主体区域-->
        <el-form :model="editSiteForm">
            <el-form-item label="友链名称">
              <el-input v-model="editSiteForm.blogName"></el-input>
            </el-form-item>
            <el-form-item label="头像">
              <el-input v-model="editSiteForm.avatar"></el-input>
            </el-form-item>
            <el-form-item label="友链描述">
              <el-input v-model="editSiteForm.description"></el-input>
            </el-form-item>
            <el-form-item label="友链地址">
              <el-input v-model="editSiteForm.url"></el-input>
            </el-form-item>
        </el-form>
        <!--内容底部区域-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="edit_site()">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      sites: [],
      current: 0,
      size: 1,
      total: 1,
      addDialogVisible: false,
      editDialogVisible: false,
      SiteForm: {
        id: 0,
        blogName: '',
        avatar: '',
        description: '',
        url: '',
        isAdded: false
      },
      editSiteForm: {
        id: '',
        blogName: '',
        avatar: '',
        description: '',
        url: '',
        isAdded: false
      }
    }
  },
  created () {
    this.getSite()
  },
  methods: {
    handleCurrentChange (currentPage) {
      this.$axios.get('friend/getSites', {
        params: {
          current: currentPage,
          size: 20
        }
      }).then(response => {
        // console.log(response.data)
        this.$set(this, 'sites', response.data.data.records)
        this.total = response.data.data.total
        this.pageSize = response.data.data.size
        this.current = response.data.data.current
      }).catch(error => {
        console.log(error)
      })
    },
    open_edit_site (row) {
      this.editDialogVisible = true
      this.editSiteForm = row
    },
    edit_site () {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.post('/friend/updateSite', this.editSiteForm).then(response => {
        if (response.data.code !== '200') this.$message.error('修改失败')
        else this.$message.success('修改成功')
        this.getSite()
        this.editDialogVisible = false
      })
    },
    siteStateChange (id) {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.get('/friend/updateStatus', {
        params: {
          sid: id
        }
      }).then(response => {
        if (response.data.code !== '200') this.$message.error('修改失败')
        else this.$message.success('修改成功')
        // this.getTags()
      })
    },
    addDialogClosed () {
      this.$refs.TagFormRef.resetFields()
    },
    editDialogClosed () {
      this.$refs.editTagFormRef.resetFields()
    },
    deleteSite (id) {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.post('/tag/deleteTag', {
        tag_id: id
      }).then(response => {
        if (response.data.code !== '200') this.$message.error('删除失败')
        else this.$message.success('删除成功')
        this.getSite()
      })
    },
    getSite () {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.get('/friend/getSites', {
        params: {
          current: 1,
          size: 20
        }
      }).then(response => {
        // console.log(response.data)
        this.$set(this, 'sites', response.data.data.records)
        this.total = response.data.data.total
        this.size = response.data.data.size
        this.current = response.data.data.current
      }).catch(error => {
        console.log(error)
      })
    },
    addSite () {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.post('/friend/addSite', this.SiteForm).then(response => {
        if (response.data.code !== '200') this.$message.error('添加失败')
        else this.$message.success('添加成功')
        this.getSite()
      })
      this.addDialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>

</style>
